<?php
class OutputComponent extends Object{

	var $name;
	var $controller;
	var $view;
	
	function arrayForSelect($dataArray, $model, $key, $value)
    {
		$array = array();
		foreach($dataArray as $data)
		{
			$array[$data[$model][$key]] = $data[$model][$value];
		}
		return $array;
	}

	function randomArrayKey($array) 
	{
       $max_keys = count($array)-1;
       $random_key = mt_rand(0, $max_keys);
       if(isset($array[$random_key]))
       {
       	return $array[$random_key];
       }
       return false;
   	} 
	
	function swRender(&$controller,$layout=null)
	{
		ob_start();
		$controller->render($action,$layout);
		$sw = ob_get_clean();
		if(DEBUG == 0)
		{
			$sw = preg_replace('/\s\s+/', ' ', $sw);
			$sw = preg_replace('/<(?:!(?:--[\s\S]*?--\s*)?(>)\s*|(?:script|style|SCRIPT|STYLE)[\s\S]*?<\/(?:script|style|SCRIPT|STYLE)>)/','',$sw);		
		}
		return $sw;
	}
	
	function render(&$controller,$action,$layout)
	{
		$controller->autoRender = false;
		$controller->params['bare'] = 1;
		$controller->action = $action;
		$controller->{$action}();
		ob_start();
		$controller->render(null,$layout);
		$return = ob_get_clean();
		return $return;
	
	}

	function sqlDateTime($data, $tag) 
	{
		if($data[$tag.'_meridian'] == 'pm' && $data[$tag.'_hour'] < 12)
		{
			$hour = $data[$tag.'_hour'] + 12;
		}
		if($data[$tag.'_meridian'] == 'am' && $data[$tag.'_hour'] == '12')
		{
			$hour = $data[$tag.'_hour'] + 12;
		}
		if($data[$tag.'_meridian'] == 'am' && $data[$tag.'_hour'] < '10')
		{
			$hour = '0'.$data[$tag.'_hour'];
		}
		$dateTime = $data[$tag.'_year'].'-'.$data[$tag.'_month'].'-'.$data[$tag.'_day'].' '.$hour.':'.$data[$tag.'_min'].':00';
		return $dateTime;
	}
	
	function br2nl(&$data) 
	{
		if(is_array($data))
		{
			$filtered = array();
			foreach($data as $key=>$value)
			{
				$data[$key] = preg_replace( '!<br.*>!iU', "\n", $value );
			}
		}
		else
		{
			$data = preg_replace( '!<br.*>!iU', "\n", $data );
		}
	}

	function htmlspecialchars(&$data, $unsc = array())
	{
		if(is_array($data))
		{
			foreach($data as $key=>$value)
			{
				if(!in_array($key, $unsc)){
					$data[$key] = $this->htmlspecialchars($value, $unsc);
				}
			}
		}
		else
		{
			$data = htmlspecialchars($data);
		}

		return $data;
	}
	
	function htmlspecialchars_decode(&$data)
	{
		if(is_array($data))
		{
			foreach($data as $key=>$value)
			{
				$data[$key] = $this->htmlspecialchars_decode($value);
			}
		}
		else
		{
			$data = htmlspecialchars_decode($data);
		}

		return $data;
	}

	function filter(&$data)
	{
		$search = array (
				 '@<script[^>]*?>.*?</script>@si', // Strip out javascript
				 '@<object[^>]*?>.*?</object>@si', // Strip out objects
				 '@<iframe[^>]*?>.*?</iframe>@si', // Strip out iframes
				 '@<applet[^>]*?>.*?</applet>@si', // Strip out applets
				 '@<meta[^>]*?>.*?</meta>@si', // Strip out meta
				 '@<form[^>]*?>.*?</form>@si', // Strip out forms
                 '@([\n])@',                // convert to <br/>
                 '@&(quot|#34);@i',                // Replace HTML entities
                 '@&(amp|#38);@i',
                 '@&(lt|#60);@i',
                 '@&(gt|#62);@i',
                 '@&(nbsp|#160);@i',
                 '@&(iexcl|#161);@i',
                 '@&(cent|#162);@i',
                 '@&(pound|#163);@i',
                 '@&(copy|#169);@i',
                 '@&#(\d+);@e');                    // evaluate as php

		$replace = array ('','','','','','','<br/>','"','&','<','>',' ',chr(161),chr(162),chr(163),chr(169),'chr(\1)');
		if(is_array($data))
		{
			$filtered = array();
			foreach($data as $key=>$value)
			{
				$data[$key] = preg_replace($search, $replace, $value);
			}
		}
		else
		{
			$data = preg_replace($search, $replace, $data);
		}
				
	}

	function refresh($url='/',$time='1')
	{
		echo '<meta http-equiv="refresh" content="'.$time.'; url="'.$url.'">';
	}
	
}

?>